﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using com.iss.inventory.Controllers;

namespace Staionery_Inventory.Presentation.Admin
{
    public partial class UserMaintenanceUI__Reload_ : System.Web.UI.Page
    {
        UserMaintenanceController empCon = new UserMaintenanceController();
        public List<String> allEmpID;
        private int pointer = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            //security and current user name
            if (Session["empID"] == null)
            {
                Response.Redirect("~/Presentation/login.aspx");
            }

            Label lbl = this.Master.FindControl("lblLogInName") as Label;
            if (lbl != null)
            {
                lbl.Text = Session["UserName"].ToString();
            }

            allEmpID = empCon.getAllEmpID();
            if (Session["currentPointer"] == null)
            {
                lblMessage.Text = "Click ◀ and ▶ button for sequence browsing";
                Session["currentPointer"] = 0;
                String[] emp = empCon.getEmpByID(allEmpID[pointer].ToString());
                setDataToTextboxes(emp);
            }
            pointer = Convert.ToInt32(Session["currentPointer"]);
        }

        public void setDataToTextboxes(String[] emp)
        {
            txtEmployeeID.Text = emp[0]; txtName.Text = emp[1]; ddlRole.Text = emp[2]; txtDeptID.Text = emp[3]; txtHeadId.Text = emp[4]; txtPhone.Text = emp[5]; txtEmail.Text = emp[6];
        }

        public void clearFields()
        {
            txtEmployeeID.Text = ""; txtName.Text = ""; ddlRole.Text = "Choose a Role"; txtDeptID.Text = ""; txtHeadId.Text = ""; txtPhone.Text = ""; txtEmail.Text = "";
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            if (pointer == 0) { pointer = allEmpID.Count(); }
            pointer -= 1;
            String[] employee = empCon.getEmpByID(allEmpID[pointer].ToString());
            Session["currentPointer"] = Convert.ToString(pointer);
            setDataToTextboxes(employee);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (pointer == allEmpID.Count() - 1) { pointer = -1; }
            pointer += 1;
            String[] employee = empCon.getEmpByID(allEmpID[pointer].ToString());
            Session["currentPointer"] = Convert.ToString(pointer);
            setDataToTextboxes(employee);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            String result = empCon.deleteEmp(txtEmployeeID.Text);
            allEmpID = empCon.getAllEmpID();
            if (pointer != 0) { pointer -= 1; }
            Session["currentPointer"] = pointer;
            String[] emp = empCon.getEmpByID(allEmpID[pointer].ToString());
            setDataToTextboxes(emp);

            lblMessage.ForeColor = Color.Black;
            lblMessage.Text = result;
        }

        protected void btnUpdateUser_Click(object sender, EventArgs e)
        {
            if (ddlRole.Text == "Choose a Role")
            {
                lblMessage.ForeColor = Color.Red;
                lblMessage.Text = "Please choose a role";
            }
            else {
                if (btnUpdateUser.Text == "Update")
                {
                    txtName.ReadOnly = false; ddlRole.Enabled = true; txtDeptID.ReadOnly = false; txtHeadId.ReadOnly = false; txtPhone.ReadOnly = false; txtEmail.ReadOnly = false;
                    btnUpdateUser.Text = "Save";
                }
                else if (btnUpdateUser.Text == "Save")
                {
                    String result = empCon.updateEmp(txtEmployeeID.Text, txtName.Text, ddlRole.Text, txtDeptID.Text, txtHeadId.Text, txtPhone.Text, txtEmail.Text);
                    if (result == "Update User Successfully")
                    {
                        lblMessage.ForeColor = Color.Black;
                        lblMessage.Text = result;
                        txtName.ReadOnly = true; ddlRole.Enabled = false; txtDeptID.ReadOnly = true; txtHeadId.ReadOnly = true; txtPhone.ReadOnly = true; txtEmail.ReadOnly = true;
                        btnUpdateUser.Text = "Update";
                        allEmpID = empCon.getAllEmpID();
                    }
                    else
                    {
                        lblMessage.ForeColor = Color.Red;
                        lblMessage.Text = result;
                    }
                }
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            if (ddlRole.Text == "Choose a Role")
            {
                lblMessage.ForeColor = Color.Red;
                lblMessage.Text = "Please choose a role";
            }
            else
            {
                if (btnCreate.Text == "New")
                {
                    txtName.ReadOnly = false; ddlRole.Enabled = true; txtDeptID.ReadOnly = false; txtHeadId.ReadOnly = false; txtPhone.ReadOnly = false; txtEmail.ReadOnly = false;
                    clearFields();
                    btnCreate.Text = "Save";
                }
                else if (btnCreate.Text == "Save")
                {
                    String result = empCon.createEmp(txtName.Text, ddlRole.Text, txtDeptID.Text, txtHeadId.Text, txtPhone.Text, txtEmail.Text);
                    if (result == "Create User Successfully")
                    {
                        allEmpID = empCon.getAllEmpID();
                        lblMessage.ForeColor = Color.Black;
                        lblMessage.Text = result;
                        txtEmployeeID.Text = allEmpID.Last().ToString();
                        txtName.ReadOnly = true; ddlRole.Enabled = false; txtDeptID.ReadOnly = true; txtHeadId.ReadOnly = true; txtPhone.ReadOnly = true; txtEmail.ReadOnly = true;
                        btnCreate.Text = "New";
                        Session["currentPointer"] = allEmpID.Count() - 1;
                    }
                    else
                    {
                        lblMessage.ForeColor = Color.Red;
                        lblMessage.Text = result;
                    }
                }
            }
        }

        protected void btnUpdatePassword_Click(object sender, EventArgs e)
        {
            String result = empCon.resetPassword(txtEmployeeID.Text);
            lblMessage.ForeColor = Color.Black;
            lblMessage.Text = result;
        }

        protected void btnUpdateDelegate_Click(object sender, EventArgs e)
        {
            String result = empCon.resetDelegate(txtEmployeeID.Text);
            lblMessage.ForeColor = Color.Black;
            lblMessage.Text = result;
        }        
    }
}